package com.itheima.youdianda.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.youdianda.common.R;
import com.itheima.youdianda.entity.YddCate;
import com.itheima.youdianda.entity.YddLink;
import com.itheima.youdianda.service.YddCateService;
import com.itheima.youdianda.util.MyPages;
import lombok.experimental.Accessors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2022-05-24
 */
@RestController
@RequestMapping("/ydd_cate")
public class YddCateController {
	@Autowired
	private YddCateService yddCateService;

	@GetMapping("/list")
	public R<MyPages<YddCate>> list(String catename, @RequestParam(defaultValue = "1") String page, @RequestParam(defaultValue = "10") String limit){
		MyPages<YddCate> myPages = yddCateService.listByPage(catename, page, limit);
		return R.success(myPages);
	}

	@GetMapping("/rowInfo")
	public R<YddCate> rowInfo(String id){
		YddCate yddCate = yddCateService.getById(id);
		return R.success(yddCate);
	}

	@PostMapping("/add")
	public R<String> add(@RequestBody YddCate yddCate){
		yddCate.setDefault_data("0");
		yddCateService.save(yddCate);
		return R.success("新增成功!");
	}

	@PostMapping("/update")
	public R<String> update(@RequestBody YddCate yddCate){
		yddCateService.updateById(yddCate);
		return R.success("修改成功!");
	}

	@GetMapping("/del")
	public R<String> del(int id){
		YddCate yddCate = yddCateService.getById(id);
		// 是默认数据,不可删除
		if("1".equals(yddCate.getDefault_data())){
			return R.success("系统默认数据，不允许删除",1000);
		}
		yddCateService.removeById(id);
		return R.success("");
	}

	@GetMapping("/all")
	public R<List<YddCate>> all(){
		LambdaQueryWrapper<YddCate> queryWrapper=new LambdaQueryWrapper<>();
		queryWrapper.orderByDesc(YddCate::getSort_num);
		List<YddCate> list = yddCateService.list(queryWrapper);
		return R.success(list);
	}
}
